package com.tanlu.tcc;

import com.tanlu.tcc.domain.InvocationContext;
import com.tanlu.tcc.util.FactoryBuilder;
import com.tanlu.tcc.util.StringUtils;

import java.io.Serializable;
import java.lang.reflect.Method;

/**
 * Created by changmingxie on 10/30/15.
 */
public class Terminator implements Serializable {

    private static final long serialVersionUID = -164958655471605778L;


    public Terminator() {

    }

    public Object invoke(InvocationContext invocationContext) throws Exception {


        if (StringUtils.isNotEmpty(invocationContext.getMethodName())) {

            try {
                Class targetClass = Class.forName(invocationContext.getClassName());
                Object target = FactoryBuilder.factoryOf(targetClass).getInstance();

                Method method = null;

                method = target.getClass().getMethod(invocationContext.getMethodName(), invocationContext.getParameterTypes());

                return method.invoke(target, invocationContext.getArgs());

            } catch (Exception e) {
                e.printStackTrace();
                throw new Exception();
            }
        }
        return null;
    }
}
